package gqz.mvvm.view;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import gqz.mvvm.R;

/**
 * Created by Administrator on 2018/1/10.
 */

public class AdapterActivity extends BaseActivity implements View.OnClickListener {

    ListView lv;
    Button btn_change, btn_update;
    MyAdapter adapter;
    ArrayList<Person> data = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adapter);

        lv = (ListView) findViewById(R.id.adapter_lv);
        btn_change = (Button) findViewById(R.id.adapter_btn_change);
        btn_update = (Button) findViewById(R.id.adapter_btn_update);
        btn_change.setOnClickListener(this);
        btn_update.setOnClickListener(this);

        initData();
        adapter = new MyAdapter(this, data);
        lv.setAdapter(adapter);
    }

    private void initData() {
        Person p;
        data.clear();
        for (int i = 0; i < 20; i++) {
            p = new Person();
            p.name = "";
            p.age = (int) (Math.random() * 100);
            p.height = Math.random() * 100 + 100;
            p.weight = Math.random() * 50 + 50;
            data.add(p);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.adapter_btn_change:
                initData();
                break;
            case R.id.adapter_btn_update:
                adapter.notifyDataSetChanged();
                break;
        }
    }
}

class MyAdapter extends BaseAdapter {

    ArrayList<Person> list = new ArrayList<>();
    Context context;

    public MyAdapter(Context context, ArrayList<Person> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.item_myadpter, null);
            holder.tv_name = (TextView) convertView.findViewById(R.id.item_myadapter_tv_name);
            holder.tv_age = (TextView) convertView.findViewById(R.id.item_myadapter_tv_age);
            holder.tv_height = (TextView) convertView.findViewById(R.id.item_myadapter_tv_height);
            holder.tv_weight = (TextView) convertView.findViewById(R.id.item_myadapter_tv_weight);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /** **/
        Person p = list.get(position);
        holder.tv_name.setText(p.name);
        holder.tv_age.setText(p.age + "");
        holder.tv_height.setText(p.height + "");
        holder.tv_weight.setText(p.weight + "");
        return convertView;
    }

    class ViewHolder {
        TextView tv_name, tv_age, tv_height, tv_weight;
    }
}

class Person {
    String name;
    int age;
    double height;
    double weight;
}
